using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Sharpie.Libs
{
	/// <summary>
	/// This is a game component that implements IUpdateable.
	/// </summary>
	public class ButtonMenu : Microsoft.Xna.Framework.DrawableGameComponent
	{
		private List<LabelButton> buttonList = new List<LabelButton>();
		int clickedPos = -1;
		Point _location;

		public List<LabelButton> Buttons { get { return buttonList; } }
		public int ClickedPos
		{
			get
			{
				int clpos = clickedPos;
				clickedPos = -1;
				return clpos;
			}
		}
		public LabelButton ClickedButton { get { return buttonList[ClickedPos]; } }
		public bool AutoWidth { get; set; }
		private int _width;
		public bool snapToEdges = true;

		public Point Location
		{
			get
			{
				return _location;
			}
			set
			{
				if (snapToEdges)
				{
					if (value.Y < 0)
						value.Y = 0;
					if (value.Y > GraphicsDevice.Viewport.Height - buttonT.Height)
						value.Y = GraphicsDevice.Viewport.Height - buttonT.Height;
					if (value.X < 0)
						value.X = 0;
					if (value.X > GraphicsDevice.Viewport.Width - Width)
						value.X = GraphicsDevice.Viewport.Width - Width;
				}
				_location = value;
			}
		}

		/// <summary>
		/// Width = 0 enables AutoWidth.
		/// </summary>
		public int Width
		{
			get
			{
				if (AutoWidth) return GraphicsDevice.Viewport.Width;
				else return _width;
			}
			set
			{
				if (value == 0) AutoWidth = true;
				else AutoWidth = false;
				_width = value;
			}
		}

		Texture2D buttonT, buttonEdgeT;
		SpriteFont buttonFont;
		public Rectangle Rectangle
		{
			get
			{
				return new Rectangle(Location.X, Location.Y, Width, buttonT.Height);
			}
		}

		new public bool Enabled { get; set; }
		public ButtonMenu(Point location, Game game)
			: base(game)
		{
			Initialize();
			Location = location;
			AutoWidth = true;
			Enabled = true;
		}

		public enum DockPosition
		{
			Top,
			Bottom,
			Left,
			Right,
			None
		}

		/// <summary>
		/// Allows the game component to perform any initialization it needs to before starting
		/// to run.  This is where it can query for any required services and load content.
		/// </summary>
		public override void Initialize()
		{
			// TODO: Add your initialization code here
			base.Initialize();
		}

		protected override void LoadContent()
		{
			base.LoadContent();
			buttonT = Game.Content.Load<Texture2D>("Images\\button");
			buttonEdgeT = Game.Content.Load<Texture2D>("Images\\buttonedge");
			buttonFont = Game.Content.Load<SpriteFont>("Fonts\\11pt");
		}

		/// <summary>
		/// Allows the game component to update itself.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		public override void Update(GameTime gameTime)
		{
			// TODO: Add your update code here
			int buttonwidth = (Width - 10 * buttonList.Count - 10) / buttonList.Count;
			if (buttonList.Count > 0)
			{
				for (int i = 0; i < buttonList.Count; i++)
				{
					if (Enabled)
						buttonList[i].Update(gameTime);
					if (buttonList[i].Clicked) clickedPos = i;
					buttonList[i].Position = new Point(i * (buttonwidth + 10) + 10 + Location.X, Location.Y);
					buttonList[i].FixedWidth = buttonwidth;
					buttonList[i].TextColor = buttonList[i].TextColor;
					buttonList[i].BackColor = buttonList[i].BackColor;
				}
			}

			base.Update(gameTime);
		}

		public override void Draw(GameTime gameTime)
		{
			foreach (LabelButton b in Buttons)
			{
				b.Draw(gameTime);
			}
			base.Draw(gameTime);
		}

		public void AddButton(LabelButton.Type Type)
		{
			buttonList.Add(new LabelButton(Type, buttonFont, buttonT, buttonEdgeT, Game));
		}

		public void AddButton(LabelButton.Type Type, string Text)
		{
			AddButton(Type);
			buttonList.Last().Text = Text;
		}

		public void AddButton(LabelButton.Type Type, string Text, Color backColor, Color textColor)
		{
			AddButton(Type, Text);
			buttonList.Last().BackColor = backColor;
			buttonList.Last().TextColor = textColor;
		}
		public void RemoveButton(int index)
		{
			buttonList.RemoveAt(index);
		}

		public void Text(int index, string text)
		{
			buttonList[index].Text = text;
		}

		public void ChangePosition(int index, int insertPosition)
		{
			using (LabelButton b = buttonList[index])
			{
				buttonList.RemoveAt(index);
				buttonList.Insert(insertPosition, b);
			}
		}

		public void ButtonColor(int index, Color color)
		{
			buttonList[index].BackColor = color;
		}

		public void TextColor(int index, Color color)
		{
			buttonList[index].TextColor = color;
		}

		public enum Direction
		{
			Up,
			Down,
			Left,
			Right
		}
	}
}
